/*
    Object.defineProperty 监听对象的读写过程
        -只能监听对象属性的读写   
*/

/*
    proxy 功能更为强大
        -能监听到更多的对象操作 见图片proxy.png
        -是以非侵入的方式监管了对象的读写
*/

const person = {
  name: "caozy",
  age: 18,
};
const personProxy = new Proxy(person, {
  get(target, property) {
    // return 100;
    return property in target ? target[property] : "default";
  },
  set(target, property, value) {
    console.log(target, property, value);
    target[property] = value;
  },
  deleteProperty(target, property) {
    console.log("delete", property);
    delete target[property];
  },
});
console.log(personProxy.name);
console.log(personProxy.sdd);
personProxy.sex = "nan";
delete personProxy.age;

/*
    proxy 监听数组
*/
let list = [];
const listProxy = new Proxy(list, {
  set(target, property, value) {
    target[property] = value;
    return true; //表示设置成功
  },
});
listProxy.push(100);
